<template>
  <el-dialog
    append-to-body
    title="选择文件"
    :visible.sync="SelectTheFileData.dialog"
    width="70%"
    :before-close="handleClose"
  >
    <el-select
      v-model="userGroupId"
      placeholder="请选择部门"
      @change="getRepository"
    >
      <el-option
        v-for="(item,index) in headList"
        :key="index"
        :label="item.userGroupName"
        :value="item.userGroupId"
      />
    </el-select>
    <el-select
      v-if="userGroupId !== ''"
      v-model="repositoryId"
      placeholder="选择知识库"
      @change="getListTreeFolder"
    >
      <el-option
        v-for="item in repository"
        :key="item.repositoryId"
        :label="item.repositoryName"
        :value="item.repositoryId"
      />
    </el-select>
    <el-cascader
      v-if="repositoryId != ''"
      ref="cascaderDocuemtn"
      :key="value"
      v-model="documentId"
      placeholder="请选择文档"
      :show-all-levels="false"
      :options="options"
      @change="getCheckedNodes"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="SelectTheFileData.dialog = false">取 消</el-button>
      <el-button type="primary" @click="getSavedata">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getsearch, getBranchList } from '@/api/uc/search'

import { listUserGroupAll } from '@/api/uc/userGroup'
import { list } from '@/api/fd/repository'
import { listTreeFolder, listTreeFolderOnlyFolder } from '@/api/fd/folder'
export default {
  props: ['SelectTheFileData'],
  data() {
    return {
      casca: { checkStrictly: true },
      value: '',
      headList: [

      ],
      userGroupId: '',
      repository: [],
      repositoryId: '',
      options: [],
      documentId: '',
      documentName: [],
      dcmid: [],
      dcmname: []
    }
  },
  mounted() {
    this.getBranchList()
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 部门获取
    // 部门获取
    async getBranchList() {
      this.headList = await getBranchList()
      const data = await listUserGroupAll({ type: 0 })
      this.headList = data.data
    },
    getRepository(val) {
      // 当部门发生改变时触发
      this.repository = []
      this.repositoryId = ''
      var data = {
        belongTo: val,
        belongType: 1
      }
      list(data).then((res) => {
        this.repository = res.data
      })
    },

    getListTreeFolder(val) {
      // 当知识库发生改变时触发
      this.options = []
      // this.temp.documentName = null
      listTreeFolder({ repositoryId: val }).then((res) => {
        if (res.data.data) {
          this.options = res.data.data
        }
      })
    },
    getCheckedNodes(data) {
      this.documentId = data[data.length - 1]
      this.documentName = this.$refs[
        'cascaderDocuemtn'
      ].getCheckedNodes()[0].label
    },
    getSavedata() {
      this.dcmid = [...this.dcmid, this.documentId]
      this.dcmname = [...this.dcmname, this.documentName]
      const Savedata = { id: this.dcmid, Name: this.dcmname }
      this.SelectTheFileData.dialog = false
      this.$emit('getSavedata', Savedata)
      this.documentId = ''
    }
  }
}
</script>

<style>
</style>
